﻿using Abp.AspNetCore.Mvc.Authorization;
using Abp.Extensions;
using Abp.IO;
using Abp.IO.Extensions;
using Abp.UI;
using Abp.Web.Models;
using Microsoft.AspNetCore.Mvc;
using Sunlit.Business.Controllers;
using Sunlit.Business.Helpers;
using Sunlit.Business.IO;
using Sunlit.Business.Media.Dto;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace Sunlit.Business.Web.Host.Controllers
{
    public class PictureController: BusinessControllerBase
    {
        private readonly IAppFolders _appFolders;
        private const int MaxHonorPictureSize = 5242880; //5MB

        public PictureController(IAppFolders appFolders)
        {
            _appFolders = appFolders;
        }
        [HttpPost]
        public UploadPictureOutput UploadPicture(PictureType pictureType = PictureType.Other)
        {
            try
            {
                var pictureFile = Request.Form.Files.First();
                if (pictureFile == null)
                {
                    throw new UserFriendlyException("请求中不包含上传图片");
                }
                // 检查文件大小
                if (pictureFile.Length > MaxHonorPictureSize)
                {
                    throw new UserFriendlyException($"只能上传{MaxHonorPictureSize / 1204 / 1024}M以下的图片");
                }
                byte[] fileBytes;
                using (var stream = pictureFile.OpenReadStream())
                {
                    fileBytes = stream.GetAllBytes();
                }

                if (!ImageFormatHelper.GetRawImageFormat(fileBytes).IsIn(ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif))
                {
                    throw new UserFriendlyException("上传图片的格式不正确，只允许上传后缀名为:\"jpg,png,gif\"的文件");
                }
                // 创建文件类型目录
                var pictureTypePath = Path.Combine(_appFolders.PictureUpladFolder, pictureType.ToString());
                DirectoryHelper.CreateIfNotExists(pictureTypePath);

                // 生成保存图片的文件名
                var userHonorFileWithoutExt = pictureType.ToString() + "_" + Guid.NewGuid().ToString("N");
                //Delete old temp profile pictures
                //AppFileHelper.DeleteFilesInFolderIfExists(userHonorPath, userHonorFileWithoutExt);

                // 保存图片
                var fileInfo = new FileInfo(pictureFile.FileName);
                var saveFilePath = Path.Combine(pictureTypePath, userHonorFileWithoutExt + fileInfo.Extension);
                System.IO.File.WriteAllBytes(saveFilePath, fileBytes);

                using (var bmpImage = new Bitmap(saveFilePath))
                {
                    return new UploadPictureOutput
                    {
                        FileName = userHonorFileWithoutExt + fileInfo.Extension,
                        Width = bmpImage.Width,
                        Height = bmpImage.Height,
                        Url = "/" + AppFolders.PicturePathName + "/" + pictureType.ToString() + "/" + userHonorFileWithoutExt + fileInfo.Extension,
                        Location = "/" + AppFolders.PicturePathName + "/" + pictureType.ToString() + "/" + userHonorFileWithoutExt + fileInfo.Extension
                    };
                }

            }
            catch (Exception ex)
            {

                return new UploadPictureOutput(new ErrorInfo(ex.Message));
            }
        }
        /// <summary>
        /// 删除用户图片
        /// </summary>
        /// <param name="PictureUrl">图片url</param>
        public void DeletePicture(string PictureUrl)
        {
            if (!string.IsNullOrWhiteSpace(PictureUrl))
            {
                var fileNameWithoutExt = Path.GetFileNameWithoutExtension(PictureUrl);
                var pictureType = fileNameWithoutExt.Split("_").First();
                var userHonorPath = Path.Combine(_appFolders.PictureUpladFolder, pictureType);
                AppFileHelper.DeleteFilesInFolderIfExists(userHonorPath, fileNameWithoutExt);
            }
        }
        [HttpPost]
        public ErrorInfo Test()
        {
            return new ErrorInfo("test");
        }
    }
}
